﻿

using Org.BouncyCastle.Asn1.Mozilla;
using System.Collections.Generic;

namespace LS.Nc6.Core
{
    public class TextSearchProvider
    {
        private static readonly Lazy<TextSearchProvider>
           lazy =
               new Lazy<TextSearchProvider>
                   (() => new TextSearchProvider());

        public static TextSearchProvider Instance { get { return lazy.Value; } }

        public readonly StringSearch stringSearch;
        public readonly WordsSearch wordsSearch;

        private TextSearchProvider()
        {
            stringSearch = new StringSearch();
            wordsSearch = new WordsSearch();
        }
        public void SetKeys(string path)
        {
            var allKeys = new List<string>();
            using (StreamReader sw = new StreamReader(File.OpenRead(path)))
            {
                string key = sw.ReadLine();
                while (key != null)
                {
                    if (key != string.Empty)
                    {
                        allKeys.Add(key);
                    }
                    key = sw.ReadLine();
                }
            }
            stringSearch.SetKeywords(allKeys);
            wordsSearch.SetKeywords(allKeys);
        }

        public bool ContainsAny(string text)
        {
            return stringSearch.ContainsAny(text);
        }

        public string FindFirst(string text)
        {
            return stringSearch.FindFirst(text);
        }

       public List<string> FindAll(string text)
        {
            return stringSearch.FindAll(text);
        }

        public string Replace(string text, char replaceChar = '*')
        {
            return  stringSearch.Replace(text);
        }

        public WordsSearchResult FindFirstResult(string text) {
           return wordsSearch.FindFirst(text);
        }
        public List<WordsSearchResult> FindAllResult(string text)
        {
            return wordsSearch.FindAll(text);
        }
        public string ReplaceResult(string text,char replaceChar = '*')
        {
            return wordsSearch.Replace(text);
        }
    }
}
